//
//  SVerticalView.m
//  BaiYing_Thinker
//
//  Created by 鹏 朱 on 15/10/29.
//  Copyright (c) 2015年 XKJH. All rights reserved.
//

#import "SVerticalView.h"
#import "SessonContentMessage.h"
#import "AppDelegate.h"
#import "DatabaseCache.h"
#import "ChatViewController.h"

@interface SVerticalView()

@end

@implementation SVerticalView

- (void)dealloc {

    NSLog(@"dealloc");
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        myPV=[[XKPagingView alloc] initWithFrame:frame];
        myPV.direction = ATPagingViewHorizontal;
        myPV.delegate = self;
        [self addSubview:myPV];
        
    }
    return self;
}

-(void)setDs:(NSArray *)aDS{
    
    if (_ds != aDS) {
        _ds =aDS;
        myPV.openAnimator = NO;
    }
    
}

- (NSInteger)getCurIndex {
    
    return myPV.currentPageIndex;
}

-(void)setCurIndex:(NSInteger)aCurIndex{
    
    if ([self getCurIndex] != aCurIndex) {
        _curIndex = aCurIndex;
        myPV.currentPageIndex = _curIndex;
    }
}

-(void)setCurIndex:(NSInteger)aCurIndex openAnimator:(BOOL)openAnimator {
    
    if ([self getCurIndex] != aCurIndex) {
        _curIndex = aCurIndex;
        [myPV setCurrentPageIndex:aCurIndex openAnimator:openAnimator];
    }
}

- (void)reloadData {
    [myPV reloadData];
}

- (void)reloadDataAtIndex:(NSUInteger)index {
    
    ChatViewController* chatVC = (ChatViewController *)[myPV viewForPageAtIndex:index];
    if(chatVC) {
        [chatVC reloadData];
    }
}

#pragma mark - ATPagingViewDelegate

- (NSInteger)numberOfPagesInPagingView:(XKPagingView *)pagingView{
    
    return self.ds.count;
}

- (UIView *)viewForPageInPagingView:(XKPagingView *)pagingView atIndex:(NSInteger)index{
    
    UIScrollView* iv =(UIScrollView*)[pagingView dequeueReusablePage];
    if (!iv) {
        iv = [[UIScrollView alloc] initWithFrame:pagingView.frame];
    }
    iv.scrollEnabled = YES;
    iv.scrollsToTop = YES;
    iv.showsVerticalScrollIndicator = YES;
    
    return iv;
}

- (NSMutableArray *)tableViewDataSource:(XKPagingView *)pagingView atIndex:(NSInteger)index {
    
    NSUInteger newIndex = (index > self.ds.count - 1)?(self.ds.count - 1) : (index < 0 ? 0: index);

    NSMutableArray* dataSource = [self.ds objectAtIndex:newIndex];
    
    return dataSource;
}

- (void)currentPageDidChangeInPagingView:(XKPagingView *)pagingView {
    
    NSLog(@"b2:%ld",myPV.currentPageIndex);
    
}
- (void)pagesDidChangeInPagingView:(XKPagingView *)pagingView {
    
}
- (void)pagingViewWillBeginMoving:(XKPagingView *)pagingView {
    
}
- (void)pagingViewDidEndMoving:(XKPagingView *)pagingView {
        
}

@end
